import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/models/home_village_entity.dart';
import 'package:kkhousekeep/page/login/login_controller.dart';

import '../../models/home_room_entity.dart';
import '../../widget/custom_app_bar.dart';
import 'switch_houses_controller.dart';

class SwitchHousesPage extends StatelessWidget {
  const SwitchHousesPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: SwitchHousesController(),
        builder: (controller) => Container(
              decoration: const BoxDecoration(
                  image: DecorationImage(
                image: AssetImage("assets/images/home_bg.png"),
                fit: BoxFit.fill,
              )),
              child: Scaffold(
                backgroundColor: Colors.transparent,
                appBar: CustomAppBar.leftTitle(
                  title: controller.userType == UserType.property
                      ? "切换小区"
                      : "请选择你的房屋",
                  toolbarHeight: 124.h,
                  titleWeight: FontWeight.bold,
                  backgroundColor: Colors.transparent,
                  callback: () => Get.back(),
                ),
                body: _builderContentWidget(controller),
              ),
            ));
  }

  Widget _builderContentWidget(SwitchHousesController controller) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 32.w),
      child: Obx(() => ListView.builder(
            itemCount: controller.userType == UserType.property
                ? controller.villages.length
                : controller.rooms.length,
            itemBuilder: (context, index) {
              bool isEnable = false;
              String address = '';
              if (controller.userType == UserType.property) {
                HomeVillageObj village = controller.villages[index];
                isEnable = village.id == controller.id.value;
                address = village.name;
              } else {
                HomeRoomObjList room = controller.rooms[index];
                isEnable = room.roomId == controller.id.value;
                address = '${room.cityName}${room.communityName}'
                    '${room.buildingName}${room.unitName}${room.roomName}';
              }
              return InkWell(
                onTap: () => controller.performClickItemTap(index),
                child: Container(
                    margin: EdgeInsets.only(bottom: 16.h),
                    padding: EdgeInsets.only(
                        left: 40.w, top: 40.w, right: 16.w, bottom: 40.w),
                    decoration: BoxDecoration(
                      color: Colors.white.withOpacity(0.8),
                      borderRadius: BorderRadius.circular(16.w),
                      gradient: isEnable
                          ? const LinearGradient(
                              colors: [
                                Color(0xFF0a4ef1),
                                Color(0xFF7ae0e7),
                              ],
                            )
                          : null,
                    ),
                    child: Flex(
                      direction: Axis.horizontal,
                      children: [
                        isEnable
                            ? ImageIcon(
                                const AssetImage(
                                    "assets/images/home_position.png"),
                                size: 40.w,
                                color: Colors.white,
                              )
                            : Container(),
                        Expanded(
                            flex: 1,
                            child: Padding(
                              padding: EdgeInsets.only(
                                  right: 38.w, left: isEnable ? 8.w : 0),
                              child: Text(
                                address,
                                softWrap: true,
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                  fontSize: 32.sp,
                                  color: isEnable
                                      ? Colors.white
                                      : const Color(0xFF6D6D6D),
                                ),
                              ),
                            )),
                        Icon(
                          Icons.arrow_forward_ios_sharp,
                          color:
                              isEnable ? Colors.white : const Color(0XFFCECECE),
                          size: 36.w,
                        ),
                      ],
                    )),
              );
            },
          )),
    );
  }
}
